package com.um.jdy.mp.station.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.um.jdy.common.member.enums.MemberEnum;
import com.um.jdy.mp.station.entity.dto.SharingDTO;
import com.um.jdy.mp.station.entity.vo.StationSharingItemVO;
import com.um.jdy.mp.station.entity.vo.StationSharingUserVO;
import com.um.jdy.mp.station.entity.vo.UserSharingItemVO;
import com.um.jdy.mp.station.manager.StationSharingManager;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.ListParams;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 网点分成
 *
 * @author cs
 * 日期 2023/6/22
 * 版权所有 广州优匠科技有限公司
 */
@Api(tags = "网点分成")
@Validated
@RestController
@RequestMapping("/api/v1/cms/station/sharing")
@SaCheckLogin
public class StationSharingController extends BaseController<StationSharingManager> {

    /**
     * 分页列表
     * @param page_no
     * @param page_size
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    public Response<Page<StationSharingItemVO>> getPage(@ApiParam(value = "页码") @NotNull Integer page_no,
                                                        @ApiParam(value = "数据量") @NotNull Integer page_size,
                                                        @ApiParam(value = "站场名称") String name){
        PageParams params = new PageParams();
        params.setPageNo(page_no);
        params.setPageSize(page_size);
        if(!StringUtil.isEmpty(name)){
            params.getCondition().put("s.name",  StringUtil.getQueryParam(name));
        }
        return manager.getPage(params);
    }

    /**
     * 选择分成人员
     *
     * @param stationId
     * @return
     */
    @ApiOperation("选择分成人员")
    @GetMapping("/get")
    public Response<List<UserSharingItemVO>> getSharingListByParentId(@ApiParam(value = "站场id") String stationId) {
        return manager.getSharingListByParentId(stationId);
    }

    /**
     * 查询分成用户列表
     * @param name
     * @return
     */
    @ApiOperation("分佣商户列表")
    @GetMapping({"user_list"})
    public Response<List<StationSharingUserVO>> list(@ApiParam(value = "商户名称") String name) {
        ListParams params = new ListParams();
        if (!StringUtil.isEmpty(name)) {
            params.getCondition().put("m.name", StringUtil.getQueryParam(name));
        }
        return manager.getUserList(params);
    }

    /**
     * 查询分页
     * @param page_no
     * @param page_size
     * @return
     */
    @ApiOperation(value = "分佣商户分页列表")
    @GetMapping("/user_page")
    public Response<Page<StationSharingUserVO>> page(@ApiParam(value = "页码") @NotNull Integer page_no,
                                                     @ApiParam(value = "数据量") @NotNull Integer page_size,
                                                     @ApiParam(value = "商户名称") String name){
        PageParams params = new PageParams();
        params.setPageNo(page_no);
        params.setPageSize(page_size);
        params.getCondition().put("m.type", MemberEnum.UserType.sharing.name());
        if (!StringUtil.isEmpty(name)) {
            params.getCondition().put("m.name", StringUtil.getQueryParam(name));
        }
        return manager.getUserPage(params);
    }

    /**
     * 保存或更新
     * @param dto
     * @return
     */
    @ApiOperation("保存或更新")
    @PostMapping
    public Response saveEntity(@RequestBody SharingDTO dto) {
        return manager.saveEntity(dto);
    }

    /**
     * 删除分账接收方
     * @return
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public Response delete(@ApiParam(value = "主键id") @PathVariable String id){
        return manager.delete(id);
    }

}
